<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SupplierPropertiesLow
 *
 * @author fabiano
 */

require_once $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.upload.php";

class SupplierPropertiesLow {
    
  private $propertyID;
  private $geographyLevel3ID;
  private $property;
  private $address;
  private $postcodeZip;
  private $telephone;
  private $fax;
  private $resort;
  private $region;
  private $country;
  private $propertyType;
  private $rating;
  private $airport;
  private $thumbnailURL;
  private $image1URL;
  private $image2URL;
  private $image3URL;
  private $description;
  private $localImage1URL;
  private $localImage2URL;
  private $localImage3URL;
  
  private $photo_url_root = 'http://medias3.latedeals.ie/lcb/';

  function __construct ($params){
    $this->setPropertyID($params[0]);
    $this->setGeographyLevel3ID($params[1]);
    $this->setProperty($params[2]);
    $this->setAddress($params[3]);
    $this->setPostcodeZip($params[4]);
    $this->setTelephone($params[5]);
    $this->setFax($params[6]);
    $this->setresort($params[7]);
    $this->setRegion($params[8]);
    $this->setCountry($params[9]);
    $this->setPropertyType($params[10]);
    $this->setRating($params[11]);
    $this->setAirport($params[12]);
    $this->setThumbnaiURL($params[13]);
    $this->setImage1URL($params[14]);
    $this->setImage2URL($params[15]);
    $this->setImage3URL($params[16]);
    $this->setDescription($params[17]);     

            
  }
    

  public function setPropertyID($propertyID){
      $this->propertyID  = $propertyID;
  }
  
  public function getPropertyID(){
      return $this->propertyID;
  }

  public function setGeographyLevel3ID($geographyLevel3ID){
      $this->geographyLevel3ID  = $geographyLevel3ID;
  }
  
  public function getGeographyLevel3ID(){
      return $this->geographyLevel3ID;
  }

  public function setProperty($property){
      $this->property = $property;
  }
  
  public function getProperty(){
      return $this->property;
  }

  public function setAddress($address){
      $this->address  = $address;
  }
  
  public function getAddress(){
      return $this->address;
  }
  
  public function setPostCodeZip($postcodeZip){
      $this->postcodeZip  = $postcodeZip;
  }
  
  public function getPostCodeZip(){
      return $this->postcodeZip;
  }
  
  public function setTelephone($telephone){
      $this->telephone  = $telephone;
  }
  
  public function getTelephone(){
      return $this->telephone;
  }
  
  public function setFax($fax){
      $this->fax = $fax;
      
  }
  public function getFax(){
      return $this->fax;
  }
  
  public function setResort($resort){
      $this->resort = $resort;      
  }
  
  public function getResort(){
      return $this->resort;
  }
  
  public function setRegion($region){
      $this->region = $region;      
  }
  
  public function getRegion(){
      return $this->region;
  }
  
  public function setCountry($country){
      $this->country = $country;     
  }
  
  public function getCountry(){
      return $this->country;
  }
  
  public function setPropertyType($propertyType){
      $this->propertyType = $propertyType;
  }
  
  public function getPropertyType(){
      return $this->propertyType;
  }
  
  public function setRating($rating){
      $this->rating = $rating;
  }
  
  public function getRating(){
      return $this->rating;
  }
  
  public function setAirport($airport){
      $this->airport = $airport;
  }
  
  public function getAirport (){
      return $this->airport;
  }
  
  public function setThumbnaiURL($thumbnailURL){
      $this->thumbnailURL = $thumbnailURL;
  }
  
  public function getThumbnailURL(){
      return $this->thumbnailURL;
  }
  
  public function setImage1URL($image1URL){
      $this->image1URL = $image1URL;
     // $this->localImage1URL = $this->savePhoto($this->image1URL);
  }
  
  public function getImage1URL(){
      return $this->image1URL;
  }
  
  public function setImage2URL($image2URL){
      $this->image2URL = $image2URL;
      //$this->localImage2URL = $this->savePhoto($this->image2URL);
  }
  
  public function getImage2URL(){
      return $this->image2URL;
  }  
  
  public function setImage3URL($image3URL){
      $this->image3URL = $image3URL;
      //$this->localImage3URL = $this->savePhoto($this->image3URL);
  }
  
  public function getImage3URL(){
      return $this->image3URL;
  }  
  
  public function setDescription($description){
      $this->description = $description;
  }
  
  public function getDescription (){
      return $this->description;
  }
  /**
   *Insert into databaseS
   * @return type 
   */
  public function save(){

      $sqltpl = 'INSERT INTO supplier_properties_low (PropertyID, GeographyLevel3ID, Property, Address, PostcodeZip, Telephone, Fax, Resort, Region, Country, PropertyType, Rating, Airport, ThumbnailURL, Image1URL, Image2URL, Image3URL, Description, LocalImage1URL,LocalImage2URL,LocalImage3URL) values
                                                (%d,%d,"%s","%s","%s","%s","%s","%s","%s","%s","%s",%d,"%s","%s","%s","%s","%s","%s","%s","%s","%s");';
      
      $sql = sprintf($sqltpl, $this->propertyID,$this->geographyLevel3ID,$this->property,$this->address,$this->postcodeZip,
                     $this->telephone ,$this->fax,$this->resort,  $this->region ,$this->country,$this->propertyType,
                     $this->rating,$this->airport,$this->thumbnailURL,$this->image1URL,$this->image2URL,$this->image3URL,
                     $this->description, $this->localImage1URL, $this->localImage2URL, $this->localImage3URL);
            
      return mysql_query($sql) or die (mysql_error());   
  }
  
  /**
   * This mehotd is responsable for doing downlod of the photo and put it into the right folder
   * @param string $url - photo url
   * @return string 
   */
  public function savePhoto($url){
      try {
         if (! $url) {
             return $url;
         }          
         $photoName =  urldecode(basename($url));                   
         if (! file_exists(SITE_ROOT_PATH . '/lcb/'. $photoName)){
             $context = stream_context_create(array('http' => array('timeout' => 2))); 
             $data = @file_get_contents($url, 0, $context);           
             if ($data){
                 file_put_contents(SITE_ROOT_PATH . '/lcb/'.$photoName, $data); 
             }else return $url;           
         }
         return $this->photo_url_root . $photoName;

      } catch (Exception $exc) {
  
          echo $exc->getTraceAsString();
      }
  }

  public function updateLocalPhotos($params){
      $sqltpl = 'UPDATE SUPPLIER_PROPERTIES_LOW SET LocalImage1URL = "%s" ,LocalImage2URL = "%s", LocalImage3URL = "%s"
                 WHERE PropertyID = "%d"';
      
      $sql = sprintf($sqltpl, $params['LocalImage1URL'],$params['LocalImage2URL'], $params['LocalImage3URL'], $params['PropertyID'] );
      return mysql_query($sql) or die (mysql_error());         
  }
  
  public function getAllPhotos(){
      $sql = "SELECT PropertyID, Image1URL, Image2URL, Image3URL FROM SUPPLIER_PROPERTIES_LOW WHERE GeographyLevel3ID  != 0 "; // and PropertyID > 1020804";
      return apply_query($sql);
  }
  
  public function insertPhotosIntoTemporaryTable($params){
      $sqltpl = 'INSERT INTO supplier_photos_low_temporary (PropertyID, LocalImage1URL, LocalImage2URL, LocalImage3URL) VALUES('
               .$params['PropertyID']. ', "' .$params['LocalImage1URL'] .'", "'.$params['LocalImage2URL'] .'", "'.$params['LocalImage3URL'] . '")';
        $sql =    $sqltpl;
      return mysql_query($sql) or die (mysql_error());         
  }  

  
}
?>